package com.gopay.bis.nucc.expay.operation;

import java.security.Security;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class AesEncryptUtils {
    
   

	public static boolean initialized = false;

	// public static final String ALGORITHM = "AES/ECB/PKCS7Padding";
	public static final String ALGORITHM = "AES/ECB/PKCS5Padding";
	
	 public static String getRandomString(int length) { //length表示生成字符串的长度
	     
	     StringBuilder key = new StringBuilder();
	        char[] chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
	        for (int i = 0; i < length; i++) {
	            int index = (int) (Math.random() * (chars.length - 1));
	            key.append(chars[index]);
	        }
	        return key.toString();	        
	     }  

	/**
	 * @param String
	 *            str 要被加密的字符串
	 * @param byte[]
	 *            key 加/解密要用的长度为32的字节数组（256位）密钥
	 * @return byte[] 加密后的字节数组
	 */
	public static byte[] Aes256Encode(String str, byte[] key) {
		initialize();
		byte[] result = null;
		try {
			Cipher cipher = Cipher.getInstance(ALGORITHM, "BC");
			SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); // 生成加密解密需要的Key
			cipher.init(Cipher.ENCRYPT_MODE, keySpec);
			result = cipher.doFinal(str.getBytes("UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static byte[] Aes256Encode(String str, String keys) {
        initialize();
        byte[] result = null;
        try {
            byte[] key = keys.getBytes("utf-8");
            Cipher cipher = Cipher.getInstance(ALGORITHM, "BC");
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); // 生成加密解密需要的Key
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            result = cipher.doFinal(str.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

	/**
	 * @param byte[]
	 *            bytes 要被解密的字节数组
	 * @param byte[]
	 *            key 加/解密要用的长度为32的字节数组（256位）密钥
	 * @return String 解密后的字符串
	 */
	public static String Aes256Decode(byte[] bytes, byte[] key) {
		initialize();
		String result = null;
		try {
			Cipher cipher = Cipher.getInstance(ALGORITHM, "BC");
			SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); // 生成加密解密需要的Key
			cipher.init(Cipher.DECRYPT_MODE, keySpec);
			byte[] decoded = cipher.doFinal(bytes);
			result = new String(decoded, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void initialize() {
		if (initialized)
			return;
		Security.addProvider(new BouncyCastleProvider());
		initialized = true;
	}

	public static Cipher initAESCipher (byte[] key) throws Exception{
		initialize();
		Cipher cipher = Cipher.getInstance(ALGORITHM, "BC");
		SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
		cipher.init(Cipher.DECRYPT_MODE, keySpec);
		return cipher;
	}
}
